#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 将一个文件夹下图片按比例分在三个文件夹下
import os
import random
import shutil
from shutil import copy2
'''
# 数据文件夹
txtDatadir_normal = 'C:/DL/data/'
txtTrainDir = "C:/DL/data/train"  # （将训练集放在这个文件夹下）
txtValDir = 'C:/DL/data/val'  # （将验证集放在这个文件夹下）
txtTestDir = 'C:/DL/data/test'  # （将测试集放在这个文件夹下）

csvDatadir_normal = 'C:/DL/csv/'
csvTrainDir = 'C:/DL/csv/train'
csvValDir = 'C:/DL/csv/val'
csvTestDir = 'C:/DL/csv/test'
'''

txtDatadir_normal = 'data/'
txtTrainDir = 'data/train/'  # （将训练集放在这个文件夹下）
txtValDir = 'data/val/'  # （将验证集放在这个文件夹下）
txtTestDir = 'data/test/'  # （将测试集放在这个文件夹下）

csvDatadir_normal = 'csv/'
csvTrainDir = 'csv/train/'
csvValDir = 'csv/val/'
csvTestDir = 'csv/test/'

txtAllData = os.listdir(txtDatadir_normal)  # （图片文件夹）
csvAllData = os.listdir(csvDatadir_normal)

num_all_data = len(txtAllData)
num_all_csv = len(csvAllData)

index_list = list(range(num_all_data))
index_csv_list = list(range(num_all_csv))

# print("num_all_csv: " + str(num_all_csv))
# print("num_all_data: " + str(num_all_data))

# print(index_list)
# print(index_csv_list)

# 随机打乱索引列表
# random.shuffle(index_list)

if not os.path.exists(txtTrainDir):
    os.mkdir(txtTrainDir)

if not os.path.exists(txtValDir):
    os.mkdir(txtValDir)

if not os.path.exists(txtTestDir):
    os.mkdir(txtTestDir)

num = 0
# print(index_list)
loop_index = index_list[0:178]
# print(loop_index)
Train_data_num = 0
Val_data_num = 0
Test_data_num = 0

Train_csv_num = 0
Val_csv_num = 0
Test_csv_num = 0
for i in loop_index:

    txtFileName = os.path.join(txtDatadir_normal, txtAllData[i])
    print(txtFileName)
    csvFileName = os.path.join(csvDatadir_normal, csvAllData[i])
    # 训练集
    if num < 108:
        # print("<108: Train", txtAllData[i], csvAllData[i])
        if copy2(txtFileName, txtValDir):
            Train_data_num += 1
        if copy2(csvFileName, csvValDir):
            Train_csv_num += 1

    # 验证集
    elif 108 <= num < 143:
        # print("108< <143: Val", txtAllData[i], csvAllData[i])
        if copy2(txtFileName, txtValDir):
            Val_data_num += 1
        if copy2(csvFileName, csvValDir):
            Val_csv_num += 1

    # 测试集
    else:
        # print(">143: Test", txtAllData[i], csvAllData[i])
        if copy2(txtFileName, txtValDir):
            Test_data_num += 1
        if copy2(csvFileName, csvValDir):
            Test_csv_num += 1

    num += 1

# print(Train_data_num, Train_csv_num)
# print(Test_data_num, Test_csv_num)
# print(Val_data_num, Val_csv_num)
